PHP Tests
A unit-testing library for PHP that's super easy to get started with.
Should I use this over PHP Unit?
If you've never used PHP Unit before, this will be easier to get started with. If you don't mind taking probably 20 or 30 minutes to read the docs & setup your environment, PHP Unit is a better choice.
Getting Started
- Install, via terminal:
- Change to download directory:
cd ~/path/to/global/installs
- Download:
git clone https://gitlab.com/taeluf/php-tests
- Add command to
~/.bashrc
:echo "alias phptest=\"php $(pwd)/php-tests/code/cli.php\"" >> ~/.bashrc
- Change to download directory:
- Write a test (see below)
-
cd
into the root of your project directory, then runphptest
- You will be prompted for a path to a global autoload file.
- You may need to configure your project. See below.
- In your test dir, there will now be an
all-tests.html
file. Open this in your browser to properly view the results.
Write a test
Notes:
- Tests must go in
test
ortests
directory - All tests must extend from
\Taeluf\Tester
. - All methods prefixed with
test
are test functions. -
prepare
gets called before the tests. - You must call
YourClass::runAll()
for your tests to run.
namespace Tlf\Tests\Generic;
class Strings extends \Taeluf\Tester {
protected $prep;
public function prepare(){
$this->prep = "Am prep string";
}
public function testStuff(){
//just a format I like to use
return true
&& $this->compare($this->prep, "Am prep string")
&& $this->compare(1, "1")
&& !$this->compare(2, "2", true) //this test is strict, thus is false, so we flip it with !
;
}
}
Strings::runAll();
Configure your project
Notes:
- Config file must be named
tlf-test.json
- Config file must be inside your
test
dir - paths are relative to your
test
dir - All PHP files in each of your
testDirs
will berequire
d. -
autoload
must be paths to php files.- The example shows
../test-extra
... As an example of having mock objects in a dir separate from your tests
- The example shows
Sample config file. Needs to be named tlf-test.json
and be inside your test
dir.
{
"testDirs": [
".",
"Utility",
"Traits"
],
"autoload": [
"../test-extra/MockSomething.php",
"../test-extra/MockAnotherThing.php"
]
}
Everything Else
- If you want more configuration, you can write a php script & execute that directly, instead of using our built in CLI script.
- call
$this->disable()
inside a test to disable it - Call
$this->catch('\Target\Exception\Class', bool $string=false)
to expect an exception. Then docatch(\Exception $e){$this->throw($e);}
. Test fails if the exception isn't "thrown"